package com.hl.toolkit.pay.controller;

import com.alibaba.fastjson.JSONException;
import com.tycoin.account.slaveentity.*;
import com.tycoin.account.slaveservice.*;
import com.tycoin.user.slaveentity.TycoinUser;
import com.tycoin.user.slaveentity.TycoinUserBank;
import com.tycoin.user.slaveentity.TycoinUserRealnamePic;
import com.tycoin.user.slaveservice.TycoinUserBankService;
import com.tycoin.user.slaveservice.TycoinUserRealnamePicService;
import com.tycoin.user.slaveservice.TycoinUserService;
import com.util.JSONtoEntityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


@Controller
 @RequestMapping(value={"/interfaceData/synchronization"})
public class InterfaceController {

	private static final Logger logger = LoggerFactory.getLogger(InterfaceController.class);

	
	@Autowired
	private TycoinUserService tycoinUserService;
	
	@Autowired
	private TycoinAccountService tycoinAccountService;
	
	@Autowired
	private TycoinAccountCashService tycoinAccountCashService;
	
		
	
	@Autowired
	private TycoinAccountLogService tycoinAccountLogService;
	
	@Autowired
	private TycoinUserBankService tycoinUserBankService;
	
	@Autowired
	private TycoinAccountRechargeService tycoinAccountRechargeService;
	
	
	@Autowired
 	private TycoinAccountPaymentService tycoinAccountPaymentService;
	
	@Autowired
 	private TycoinUserRealnamePicService tycoinUserRealnamePicService;
	
	@Autowired
 	private TycoinEthLogService tycoinEthLogService;
	
	@Autowired
 	private TycoinEtcLogService tycoinEtcLogService;
   	
	
	
	@RequestMapping(value = "/TycoinUser", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinUser(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
  		String tycoinUserStr=request.getParameter("TycoinUser");
   		TycoinUser tycoinUser=(TycoinUser) JSONtoEntityUtil.JSONToObj(tycoinUserStr, TycoinUser.class);
 		tycoinUserService.save(tycoinUser);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
    
 	}
	
	
	@RequestMapping(value = "/findTycoinUser", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findTycoinUser(HttpServletRequest request, HttpServletResponse response)
			throws JSONException, IOException {
		  Map<String,Object> map=new HashMap<String,Object>();
	    	String userId=request.getParameter("userId");
 			TycoinUser tycoinUser=tycoinUserService.findById(Long.valueOf(userId));
   	    if(tycoinUser!=null){
     			map.put("code", "200");
  	       	map.put("tycoinUser", tycoinUser);	
   	    } else{
     			map.put("code", "201");
    	    }

 	 		return map;
    
 	}
	
	
	@RequestMapping(value = "/TycoinAccount", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinAccount(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
 		String tycoinAccountStr=request.getParameter("TycoinAccount");
  		TycoinAccount tycoinAccount=(TycoinAccount) JSONtoEntityUtil.JSONToObj(tycoinAccountStr, TycoinAccount.class);
  		tycoinAccountService.save(tycoinAccount);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
	}
	@RequestMapping(value = "/TycoinAccountCash", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinAccountCash(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
 		String tycoinAccountCashStr=request.getParameter("TycoinAccountCash");
 		TycoinAccountCash tycoinAccountCash=(TycoinAccountCash) JSONtoEntityUtil.JSONToObj(tycoinAccountCashStr, TycoinAccountCash.class);
 		tycoinAccountCashService.save(tycoinAccountCash);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
	}
	
	
	
	@RequestMapping(value = "/TycoinAccountLog", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinAccountLog(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
 		String tycoinAccountLogStr=request.getParameter("TycoinAccountLog");
 		TycoinAccountLog tycoinAccountLog=(TycoinAccountLog) JSONtoEntityUtil.JSONToObj(tycoinAccountLogStr, TycoinAccountLog.class);
  		tycoinAccountLogService.save(tycoinAccountLog);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
	}
	
	@RequestMapping(value = "/TycoinUserBank", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinUserBank(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String tycoinUserBankStr=request.getParameter("TycoinUserBank");
		TycoinUserBank tycoinUserBank=(TycoinUserBank) JSONtoEntityUtil.JSONToObj(tycoinUserBankStr, TycoinUserBank.class);
		tycoinUserBankService.save(tycoinUserBank);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
	}
	
	
	@ResponseBody
	public String getTycoinAccountRecharge(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String tycoinAccountRechargeStr=request.getParameter("TycoinAccountRecharge");
		TycoinAccountRecharge tycoinAccountRecharge=(TycoinAccountRecharge) JSONtoEntityUtil.JSONToObj(tycoinAccountRechargeStr, TycoinAccountRecharge.class);
		tycoinAccountRechargeService.save(tycoinAccountRecharge);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
	}
	
	
	@RequestMapping(value = "/findTycoinAccountRecharge", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findTycoinAccountRecharge(HttpServletRequest request, HttpServletResponse response)
			throws JSONException, IOException {
 		  Map<String,Object> map=new HashMap<String,Object>();
 	    	String transactionID=request.getParameter("transactionID");
   			TycoinAccountRecharge tycoinAccountRecharge=tycoinAccountRechargeService.findOneBypaymentId(transactionID);
     	    if(tycoinAccountRecharge!=null){
       			map.put("code", "200");
    	       	map.put("tycoinAccountRecharge", tycoinAccountRecharge);	
     	    } else{
       			map.put("code", "201");
      	    }

   	 		return map;
      
 	}
	
	
	
	@RequestMapping(value = "/TycoinAccountPayment", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinAccountPayment(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String TycoinAccountPaymentStr=request.getParameter("TycoinAccountPayment");
		TycoinAccountPayment tycoinAccountPayment=(TycoinAccountPayment) JSONtoEntityUtil.JSONToObj(TycoinAccountPaymentStr, TycoinAccountPayment.class);
		tycoinAccountPaymentService.save(tycoinAccountPayment);
 		return "{\"code\":\"0\",\"msg\":\"SUCCESS\"}";
	}
	
	
	
	
	
	
	
	
	@RequestMapping(value = "/TycoinEthLog", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinEthLog(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String tycoinEthLogStr=request.getParameter("TycoinEthLog");
		TycoinEthLog tycoinEthLog=(TycoinEthLog) JSONtoEntityUtil.JSONToObj(tycoinEthLogStr, TycoinEthLog.class);
		tycoinEthLogService.save(tycoinEthLog);
 		return "{\"code\":\"0\",\"SUCCESS\"}";
	}
	
 
	@RequestMapping(value = "/TycoinEtcLog", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinEtcLog(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String tycoinEtcLogStr=request.getParameter("TycoinEthLog");
		TycoinEtcLog tycoinEtcLog=(TycoinEtcLog) JSONtoEntityUtil.JSONToObj(tycoinEtcLogStr, TycoinEtcLog.class);
		tycoinEtcLogService.save(tycoinEtcLog);
 		return "{\"code\":\"0\",\"SUCCESS\"}";
	}
	
	
	

	@RequestMapping(value = "/TycoinUserRealnamePic", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String getTycoinUserRealname(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String tycoinUserRealnamePicStr=request.getParameter("TycoinUserRealnamePic");
		TycoinUserRealnamePic tycoinUserRealnamePic=(TycoinUserRealnamePic) JSONtoEntityUtil.JSONToObj(tycoinUserRealnamePicStr, TycoinUserRealnamePic.class);
		tycoinUserRealnamePicService.save(tycoinUserRealnamePic);
 		return "{\"code\":\"0\",\"SUCCESS\"}";
	}
 
}
